package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PictureShare;
import cn.wolfcode.trip.base.domain.ShowBean;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.PictureShareMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.FriendTravelSharePageInfo;
import cn.wolfcode.trip.base.query.FriendTravelShareQueryObject;
import cn.wolfcode.trip.base.service.IFriendsTravelShareService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 关注分享业务层
 *
 * @author Wangyk
 * @date 2019/1/14 20:10
 */
@Controller
public class FriendsTravelShareServiceImpl implements IFriendsTravelShareService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private PictureShareMapper pictureShareMapper;

    /**
     * 查询当前用户的朋友圈分页数据
     *
     * @param qo          查询条件
     * @param currentUser 当前用户信息
     * @return 分享数据分页集合
     */
    @Override
    public PageInfo getCurrentUserzFriendsTravelShare(FriendTravelShareQueryObject qo, User currentUser) {
        if (!qo.getPicturesHasNextPage() && !qo.getTravelsHasNextPage()) return null;

        //1:拿到当前用户的关注
        List<User> followList = userMapper.selectUserzFollowListByUserId(currentUser.getId());
        followList.add(currentUser);
        if (followList.size() == 0) return null;

        PageInfo<ShowBean> pageInfo1 = new PageInfo<>(new ArrayList<>()); //盛放游记list
        PageInfo<ShowBean> pageInfo2 = new PageInfo<>(new ArrayList<>()); //盛放图片分享list
        Boolean travelsHasNextPage = false;
        Boolean picturesHasNextPage = false;

        //2:分页查询关注用户的游记分享
        if (qo.getTravelsHasNextPage()) {
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "t.releaseTime desc");
            List<ShowBean> showBeanList1 = travelMapper.selectTravelShowBeanByUserIds(followList);
            pageInfo1 = new PageInfo<>(showBeanList1);
            //2,1 判断
            travelsHasNextPage = pageInfo1.isHasNextPage();
        }

        //3:查询关注者的图片分享
        if (qo.getPicturesHasNextPage()) {
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "picture_share.share_time DESC");
            List<ShowBean> showBeanList2 = pictureShareMapper.selectPictureShareByUserIds(followList);
            pageInfo2 = new PageInfo<>(showBeanList2);
            //3.2 判断
            picturesHasNextPage = pageInfo2.isHasNextPage();
        }

        //4:整理成pageInfo  5:list排序
        pageInfo1.getList().addAll(pageInfo2.getList());
        List list = listOrderByTimeDesc(pageInfo1.getList());
        FriendTravelSharePageInfo showBeanPageInfo = new FriendTravelSharePageInfo(list, travelsHasNextPage, picturesHasNextPage);

        return showBeanPageInfo;
    }

    /**
     * 集合按照元素内的时间字段 倒序排序
     *
     * @param list 要排序的集合
     * @return 排序好的集合
     */
    private List listOrderByTimeDesc(List<ShowBean> list) {

        int len = list.size();
        for (int i = 0; i <= len - 1; i++) {
            for (int j = i + 1; j <= len - 1; j++) {
                //1:取出时间
                Date jTime = list.get(j).getReleaseTime();
                Date iTime = list.get(i).getReleaseTime();
                //2:对比
                if (jTime.after(iTime)) {        // 相邻元素两两对比
                    ShowBean temp = list.get(j);        // 元素交换
                    list.set(j, list.get(i));
                    list.set(i, temp);
                }
            }
        }

        return list;
    }
}